﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using System.Windows.Resources;

namespace Tsina
{
    public partial class IMEControl : UserControl
    {
        private static Dictionary<string, char[]> pinyinDictionary = new Dictionary<string, char[]>();

        static IMEControl()
        {
            Uri uri = new Uri("/Tsina;component/pinyin.txt", UriKind.Relative);
            StreamResourceInfo sri = Application.GetResourceStream(uri);
            using (StreamReader reader = new StreamReader(sri.Stream))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] pinyinAndWord = line.Split('\t');
                    pinyinDictionary.Add(pinyinAndWord[0], pinyinAndWord[1].ToCharArray());
                }
            }
        }

        public IMEControl()
        {
            InitializeComponent();
            this.hanziButtons = new IMEButtonControl[] { word1Button, word2Button, word3Button, word4Button, word5Button };
            this.Loaded += IMEControl_Loaded;
        }

        private void IMEControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.pinyinTextBlock.Text = string.Empty;
            this.currentPageNumber = 0;
            this.backspaceButton.IsEnable = false;
            this.previousButton.IsEnable = false;
            this.nextButton.IsEnable = false;
            this.word1Button.Visibility = Visibility.Collapsed;
            this.word2Button.Visibility = Visibility.Collapsed;
            this.word3Button.Visibility = Visibility.Collapsed;
            this.word4Button.Visibility = Visibility.Collapsed;
            this.word5Button.Visibility = Visibility.Collapsed;
        }

        private void backspaceButton_Click(object sender, EventArgs e)
        {
            if (this.Pinyin.Length > 0)
            {
                this.Pinyin = this.Pinyin.Substring(0, this.Pinyin.Length - 1);
            }
        }

        private void previousButton_Click(object sender, EventArgs e)
        {
            this.currentPageNumber -= 1;
            UpdateHanziList(this.Pinyin, this.currentPageNumber);
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            this.currentPageNumber += 1;
            UpdateHanziList(this.Pinyin, this.currentPageNumber);
        }

        private void wordButton_Click(object sender, EventArgs e)
        {
            this.Pinyin = string.Empty;
            if (GotHanzi != null)
            {
                GotHanzi(sender, e);
            }
        }

        public string Pinyin
        {
            get 
            {
                return this.pinyinTextBlock.Text;
            }
            set
            { 
                if (this.pinyinTextBlock.Text != value)
                {
                    this.pinyinTextBlock.Text = value;
                    this.currentPageNumber = 0;
                    if (value == string.Empty)
                    {
                        this.backspaceButton.IsEnable = false;
                    }
                    else
                    {
                        this.backspaceButton.IsEnable = true;
                    }
                    UpdateHanziList(value, this.currentPageNumber);
                }
            }
        }

        private int currentPageNumber;

        private void UpdateHanziList(string pinyin, int pageNumber)
        {
            if (pinyinDictionary.ContainsKey(pinyin) == true)
            {
                int maxIndex = pinyinDictionary[pinyin].Length - 1;
                if (pageNumber > 0)
                {
                    this.previousButton.IsEnable = true;
                }
                else
                {
                    this.previousButton.IsEnable = false;
                }
                if ((pageNumber * 5 + 4) >= maxIndex)
                {
                    this.nextButton.IsEnable = false;
                }
                else
                {
                    this.nextButton.IsEnable = true;
                }
                for (int i = 0; i < 5; i++)
                {
                    int currentIndex = pageNumber * 5 + i;
                    if (currentIndex <= maxIndex)
                    {
                        hanziButtons[i].Visibility = Visibility.Visible;
                        hanziButtons[i].Text = (pinyinDictionary[pinyin])[currentIndex].ToString();
                    }
                    else
                    {
                        hanziButtons[i].Visibility = Visibility.Collapsed;
                    }
                }
            }
            else
            {
                this.previousButton.IsEnable = false;
                this.nextButton.IsEnable = false;
                this.word1Button.Visibility = Visibility.Collapsed;
                this.word2Button.Visibility = Visibility.Collapsed;
                this.word3Button.Visibility = Visibility.Collapsed;
                this.word4Button.Visibility = Visibility.Collapsed;
                this.word5Button.Visibility = Visibility.Collapsed;
            }
        }

        private IMEButtonControl[] hanziButtons;

        public event EventHandler GotHanzi;
    }
}
